package com.klasster.recruitmentPlatform.controller;


import com.klasster.recruitmentPlatform.common.util.PermissionUtil;
import com.klasster.recruitmentPlatform.common.response.Result;
import com.klasster.recruitmentPlatform.common.response.ResultCode;
import com.klasster.recruitmentPlatform.common.util.TokenUtil;
import com.klasster.recruitmentPlatform.domain.dto.PasswordDTO;
import com.klasster.recruitmentPlatform.domain.po.User;
import com.klasster.recruitmentPlatform.domain.vo.UserVO;
import com.klasster.recruitmentPlatform.service.impl.UserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 用户相关操作请求控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserServiceImpl userService;
    private final TokenUtil tokenUtil;
    private final PermissionUtil permissionUtil;

    /**
     * 获取用户信息
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader("token") String token) {
        Long user_id = tokenUtil.getUserID(token);
        User user = userService.getById(user_id);
        if(user==null) {
            return Result.error(ResultCode.USER_INVALID,null);
        }
        UserVO userVO = new UserVO(user.getUser_id(),user.getUsername(),user.getRole(),user.getAvatar_url());
        return Result.success(ResultCode.USER_GET_USERINFO,userVO);
    }

    /**
     * 获取角色权限
     */
    @GetMapping("/getPermissions")
    public Result getPermissions(@RequestHeader("token") String token) {
        Long user_id = tokenUtil.getUserID(token);
        User user = userService.getById(user_id);
        String role = user.getRole();
        return Result.success(ResultCode.USER_GET_PERMISSIONS,permissionUtil.getPermissions(role));
    }

    /**
     * 修改用户头像
     */
    @PutMapping("/updateAvatar")
    public Result updateAvatar(@RequestHeader("token") String token, @RequestParam String avatar_url) {
        Long user_id = tokenUtil.getUserID(token);
        User user = userService.getById(user_id);
        user.setAvatar_url(avatar_url);
        boolean result = userService.saveOrUpdate(user);
        if(result) {
            return Result.success(ResultCode.USER_UPDATE_SUCCESS,true);
        }
        return Result.error(ResultCode.USER_UPDATE_FAIL,false);
    }


    /**
     * 修改用户密码
     */
    @Transactional
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordDTO passwordDTO) {
        User user = userService.getById(passwordDTO.getUser_id());
        if(!user.getPassword().equals(passwordDTO.getOriginPsw())) {
            return Result.error(ResultCode.USER_UPDATE_PASSWORD_FAIL,false);
        }
        user.setPassword(passwordDTO.getNewPsw());
        if(userService.updateById(user)) {
            //更新密码成功，重生成token返回
            return Result.success(ResultCode.USER_UPDATE_SUCCESS,tokenUtil.generateToken(user.getUser_id(),user.getPassword()));
        }
        return Result.success(ResultCode.USER_UPDATE_FAIL,false);
    }

}
